package com.powernode.controller;

import com.powernode.domain.Category;
import com.powernode.model.Result;
import com.powernode.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品类目接口管理")
@RequestMapping("prod/category")
@RestController

public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("查询系统所有商品类目")
    @GetMapping("table")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public Result<List<Category>> loadAllCategoryList() {
        List<Category> list = categoryService.queryAllCategoryList();
        return Result.success(list);
    }

    @ApiOperation("查询商品一级类目")
    @GetMapping("listCategory")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public Result<List<Category>> loadRootCategoryList() {
        List<Category> rootCategoryList = categoryService.queryRootCategoryList();
        return Result.success(rootCategoryList);
    }

    @ApiOperation("新增商品类目")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:category:save')")
    public Result<String> saveCategory(@RequestBody Category category) {
        Integer count = categoryService.addCategory(category);
        return Result.handle(count > 0);
    }

    @ApiOperation("根据标识查询商品类目详情")
    @GetMapping("info/{categoryId}")
    @PreAuthorize("hasAuthority('prod:category:info')")
    public Result<Category> loadCategoryInfo(@PathVariable Long categoryId) {
        Category category = categoryService.getById(categoryId);
        return Result.success(category);
    }

    @ApiOperation("修改商品类目")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:category:update')")
    public Result<String> modifyCategory(@RequestBody Category category) {
        Integer count = categoryService.modifyCategory(category);
        return Result.handle(count > 0);
    }


    @ApiOperation("删除商品类目")
    @DeleteMapping("{categoryId}")
    @PreAuthorize("hasAuthority('prod:category:delete')")
    public Result<String> removeCategory(@PathVariable Long categoryId) {
        Integer count = categoryService.removeCategoryById(categoryId);
        return Result.handle(count > 0);
    }


    //////////////////////微信小程序:数据接口 ////////////////////////////
    //  product-service/prod/category/category/list
    @ApiOperation("查询小程序中商品的一级类目集合")
    @GetMapping("category/list")
    public Result<List<Category>> loadWxCategoryList(@RequestParam Long parentId) {
        List<Category> list = categoryService.queryWxCategoryListByPid(parentId);
        return Result.success(list);
    }
}
